<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="btnCansle">
    <el-checkbox-group v-model="roleIds">
      <!-- 选项 -->
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{
          item.name
        }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="btnCansle">取消</el-button>
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      default: null
    //   required: true
    }
  },
  data() {
    return {
      list: [], // 所有角色列表
      roleIds: [] // 每个员工拥有的角色的id
    }
  },
  created() {
    this.getRoleList()
  },
  // 获取角色列表
  methods: {
    async getRoleList() {
      const { rows } = await getRoleList()
      this.list = rows
    },
    // 获取每个角色的id
    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id)
      this.roleIds = roleIds // 赋值本用户的角色
    },
    // 分配角色
    async  btnOk() {
      await assignRoles({ id: this.userId, roleIds: this.roleIds })
      this.$emit('close')
    },
    btnCansle() {
      this.roleIds = []
      this.$emit('close')
    }
  }
}
</script>

<style>

</style>
